package com.zhiyou.app.shop.website.service.impl;

import java.sql.Timestamp;
import java.util.Date;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhiyou.app.shop.common.util.Page;
import com.zhiyou.app.shop.common.util.ShopConstants;
import com.zhiyou.app.shop.website.dao.IActivityApplyDao;
import com.zhiyou.app.shop.website.dao.IMoneyDealDao;
import com.zhiyou.app.shop.website.dao.IMsgDao;
import com.zhiyou.app.shop.website.dao.IUserAccountDao;
import com.zhiyou.app.shop.website.dao.IUserInfoDao;
import com.zhiyou.app.shop.website.dao.pojo.ActivityApply;
import com.zhiyou.app.shop.website.dao.pojo.MoneyDeal;
import com.zhiyou.app.shop.website.dao.pojo.Msg;
import com.zhiyou.app.shop.website.dao.pojo.User;
import com.zhiyou.app.shop.website.dao.pojo.UserAccount;
import com.zhiyou.app.shop.website.service.IActivityApplyService;
import com.zhiyou.app.shop.website.service.IUserManagerService;

@Service(value="activityApplyService")
public class ActivityApplyServiceImpl implements IActivityApplyService {

	@Autowired
	private IActivityApplyDao activityApplyDao;
	@Autowired
	private IUserAccountDao userAccountDao;
	@Autowired
	private IMoneyDealDao moneyDealDao;
	@Autowired
	private IUserInfoDao userInfoDao;
	@Autowired
	private IMsgDao msgDao;
	
	public void findApply(Page page,String status) {
		DetachedCriteria criteria = DetachedCriteria.forClass(ActivityApply.class);
		if(status != null && !"".equals(status)){
			criteria.add(Expression.eq("status", status));
		}
		activityApplyDao.findByCriteria(page, criteria, Order.desc("createDate"));
		
	}

	public ActivityApply findById(String Id) {
		return activityApplyDao.findById(Id);
	}

	//收款后 激活豆发放
	public void saveApplyInfo(String id) {
		ActivityApply apply = findById(id);
		apply.setStatus("2");
		activityApplyDao.update(apply);
		
		UserAccount account = userAccountDao.findByAccount(apply.getApplyId());
		account.setActivMoney(account.getActivMoney().add(apply.getActAmount()));
		userAccountDao.update(account);
		
		User user = userInfoDao.findByAccount(apply.getApplyId());
		MoneyDeal deal = new MoneyDeal();
		deal.setAccountSubject("申请激活豆");
		deal.setAccountType(ShopConstants.ACTIVITY);
		deal.setCreateDate(new Timestamp(new Date().getTime()));
		deal.setIncome(apply.getActAmount());
		deal.setSubjectType("");
		deal.setUserId(user.getUserId());
		deal.setRemark(user.getUserAccount());
		moneyDealDao.save(deal);
		
		//发消息
		Msg msg = new Msg();
		msg.setCreateDate(new Timestamp(new Date().getTime()));
		msg.setFlag(ShopConstants.TRUE);
		msg.setMsg("您申请的"+apply.getApplyAmount()+"个激活豆已经通过审核，并已将激活豆发放到您的账号中了");
		msg.setReceiveId(user.getUserId());
		msg.setSendId("");
		msg.setTitle("激活豆申请成功提醒");
		msg.setType("1");
		msgDao.save(msg);
	}

}
